package com.asnike.selector{
	
	import com.asnike.component.SimpleButton;
	
	import fl.controls.ScrollBar;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.getDefinitionByName;
	import flash.utils.setTimeout;
	
	public class MapSelector extends Sprite{
		
		private var _mapLoader:Loader;
		private var _placeList:CampPlaceItemList;
		private var _data:XML;
		private var _scrollBar:ScrollBar;
		private var _btnNext:SimpleButton;
		private var _clickBtnNextCallback:Function;
		private var _currentIndex:int;
		
		public function MapSelector( $data:XML ){
			setData( $data );
			initUpperImage();
			loadFlexSWF();
			initItemList();
			initScrollBar();
			initEvent();
			initButton();
		}
		private function initUpperImage():void{
			var cls:Class, search:Bitmap, dropdown:Bitmap;
			
			cls = getDefinitionByName( 'bt_search' ) as Class;
			search = new Bitmap( new cls );
			search.x = 17;
			search.y = 169;
			addChild( search );
			cls = getDefinitionByName( 'dropdown' ) as Class;
			dropdown = new Bitmap( new cls );
			dropdown.x = 554;
			dropdown.y = 169;
			addChild( dropdown );
		}
//map------------------------------------------------------------------------------------------------------------------------------		
		private function loadFlexSWF():void{
			var currentCount:int;
			
			_mapLoader = new Loader;
			_mapLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, function( $e:Event ):void{
				$e.target.removeEventListener( $e.type, arguments.callee );
				$e.target.content.addEventListener( 'applicationComplete', onApplicationComplete );
			} );
			_mapLoader.addEventListener( 'mx.managers.SystemManager.isBootstrapRoot', systemManagerHandler );
			_mapLoader.addEventListener( 'mx.managers.SystemManager.isStageRoot', systemManagerHandler );
			_mapLoader.load( new URLRequest( 'remote/NaverMap.swf' ), new LoaderContext( false, ApplicationDomain.currentDomain ) );
			addChild( _mapLoader );
		}
		private function systemManagerHandler( $e:Event ):void{
			$e.preventDefault();
		}
		private function onApplicationComplete( $e:Event ):void{
			$e.target.application.addEventListener( 'initCompleteNaverMap', initCompleteNaverMap );
		}
		private function initCompleteNaverMap( $e:Event ):void{
			trace( 'init!!!' );
			var i:int, j:int, map:INaverMap, doMap:DisplayObject;
			
			map = $e.target as INaverMap;
			map.setMarkerClickCallback( clickMarker );
			
			doMap = $e.target as DisplayObject;
			doMap.x = 17;
			doMap.y = 231;
			
			for( i = 0, j = _data.place.length() ; i < j ; ++i ){
				map.setMarker( _data.place[i].name.toString(), i.toString(), new Point( Number( _data.place[i].location[0].@lat ),Number( _data.place[i].location[0].@lon ) ) );
			}
		}
		private function clickMarker( $index:String ):void{
			trace( 'click marker : ', $index );
//			_placeList.setData( _data.place[int( $index )] );
			_placeList.showCurrentItemDetailInfo( int( $index ) );
			_currentIndex = int( $index );
		}
//item list-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		private function initItemList():void{
			var mask:Shape;
			
			_placeList = new CampPlaceItemList;
			_placeList.setData( _data );
			_placeList.x = 349;
			_placeList.y = 231;
			_placeList.setItemClickCallback( itemClick );
			addChild( _placeList );
			
			mask = new Shape;
			mask.graphics.beginFill( 0 );
			mask.graphics.drawRect( _placeList.x, _placeList.y, _placeList.width, 460 );
			mask.graphics.endFill();
			addChild( mask );
			_placeList.mask = mask;

		}
		private function itemClick( $index:int, $itemState:String ):void{
			switch( $itemState ){
			case 'shortInfo':
				_scrollBar.setScrollProperties( 0, 0, 128*4 - 331 );
				_scrollBar.visible = true;
				break;
			case 'detailInfo':
				_placeList.y = 231;
				_scrollBar.setScrollProperties( 0, 0, 0 );
				_scrollBar.visible = false;
				break;
			}
			_currentIndex = $index;
		}
		
		public function setData( $data:XML ):void{
			_data = $data;
		}
		private function setItemList( $name:String ):void{
			 
		}
		private function initScrollBar():void{
			_scrollBar = new ScrollBar;
			_scrollBar.setScrollProperties( 0, 0, 128*4 - 331 );
			_scrollBar.addEventListener( Event.SCROLL, scroll );
			_scrollBar.setSize( 10, 460 );
			_scrollBar.x = _placeList.x + _placeList.width;
			_scrollBar.y = _placeList.y;
			addChild( _scrollBar );
		}
		private function scroll( $e:Event ):void{
			_placeList.y = 231 -_scrollBar.scrollPosition;
		}
		private function initEvent():void{
			addEventListener( Event.ADDED_TO_STAGE, addedToStage );
		}
		private function addedToStage( $e:Event ):void{
			activateMouseScroll();
		}
		private function activateMouseScroll():void{
			stage.addEventListener( MouseEvent.MOUSE_WHEEL, wheel );
		}
		private function wheel( $e:MouseEvent ):void{
			if( $e.delta > 1 ){
				_scrollBar.scrollPosition -= 5;
			}else if( $e.delta < -1 ){
				_scrollBar.scrollPosition += 5;
			}
		}
		private function initButton():void{
			_btnNext = new SimpleButton;
			_btnNext.x = 640;
			_btnNext.y = 707;
			_btnNext.addEventListener( MouseEvent.CLICK, function( $e:MouseEvent ):void{
				if( _clickBtnNextCallback is Function ){
					_clickBtnNextCallback( 3 );
				}
			} );
			addChild( _btnNext );
		}
		public function setClickBtnNextCallback( $function:Function ):void{
			_clickBtnNextCallback = $function;
		}
		public function setUpperImage( $id:String ):void{
			var cls:Class, upper:Bitmap;
			
			cls = getDefinitionByName( $id ) as Class;
			upper = new Bitmap( new cls );
			addChild( upper );
		}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------		
		public function show():void{
			visible = true;
			activateMouseScroll();
		}
		public function hide():void{
			visible = false;
			inactivateMouseScroll();
		}
		private function inactivateMouseScroll():void{
			if( stage ){
				stage.removeEventListener( MouseEvent.MOUSE_WHEEL, wheel );
			}
		}
		public function getResult():int{
			return _currentIndex;
		}
	}
}